package com.shbaiche.bifenba.ui.data;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.data.DataPlayerListAdapter;
import com.shbaiche.bifenba.adapter.data.DataTeamDetailMemberListAdapter;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.data.PlayerGroupVo;
import com.shbaiche.bifenba.model.data.TeamInfoMessage;
import com.shbaiche.bifenba.model.data.TeamInfoVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;

import java.util.ArrayList;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.base.XLazyFragment;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * 球队-成员
 */

@SuppressLint("ValidFragment")
public class DataTeamDetailMemberFragment extends XLazyFragment {

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;

    DataTeamDetailMemberListAdapter adapter;

    protected static final int PAGE_SIZE = 10;
    protected static final int MAX_PAGE = 10;

    TeamInfoMessage teamInfoMessage;

    @SuppressLint("ValidFragment")
    public DataTeamDetailMemberFragment(){

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(savedInstanceState!=null && savedInstanceState.containsKey("message")){
            teamInfoMessage = (TeamInfoMessage) savedInstanceState.getSerializable("message");
        }else{
            teamInfoMessage = (TeamInfoMessage)getArguments().getSerializable("message");
        }
        initAdapter();
        loadData(1);

        if(teamInfoMessage!=null){
            ArrayList<PlayerGroupVo> list = new ArrayList<>();

            PlayerGroupVo groupJL = new PlayerGroupVo();
            groupJL.setTitle("教练");
            groupJL.setList(teamInfoMessage.getCoach());
            list.add(groupJL);

            PlayerGroupVo groupQF = new PlayerGroupVo();
            groupQF.setTitle("前锋");
            groupQF.setList(teamInfoMessage.getForward());
            list.add(groupQF);

            PlayerGroupVo groupZF = new PlayerGroupVo();
            groupZF.setTitle(teamInfoMessage.isFootball()?"中场":"中锋");
            groupZF.setList(teamInfoMessage.getMidfield());
            list.add(groupZF);

            PlayerGroupVo groupHW = new PlayerGroupVo();
            groupHW.setTitle(teamInfoMessage.isFootball()?"后卫":"后卫");
            groupHW.setList(teamInfoMessage.getGuard());
            list.add(groupHW);

            if(teamInfoMessage.isFootball()){
                PlayerGroupVo groupMJ = new PlayerGroupVo();
                groupMJ.setTitle("门将");
                groupMJ.setList(teamInfoMessage.getGoalkeeper());
                list.add(groupMJ);
            }else{
                PlayerGroupVo groupQT = new PlayerGroupVo();
                groupQT.setTitle("其他");
                groupQT.setList(teamInfoMessage.getOther());
                list.add(groupQT);
            }

            getAdapter().setData(list);

        }
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData(1);
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData(page);
                    }
                });

        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();
    }

    public void loadData(final int page) {
        contentLayout.getSwipeRefreshLayout().setRefreshing(false);

    }

    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new DataTeamDetailMemberListAdapter(context);
            adapter.setRecItemClick(new RecyclerItemCallback<PlayerGroupVo, DataTeamDetailMemberListAdapter.ViewHolder>() {
                @Override
                public void onItemClick(int position, PlayerGroupVo model, int tag, DataTeamDetailMemberListAdapter.ViewHolder holder) {
                    super.onItemClick(position, model, tag, holder);
                }
            });
        }
        return adapter;
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_data_team_detail_member;
    }

}
